package biz.obake.team.touchprotector.f;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.os.Build;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import biz.obake.team.touchprotector.R;
import biz.obake.team.touchprotector.g.a;
import biz.obake.team.touchprotector.g.c;
import biz.obake.team.touchprotector.lfd.f;
import biz.obake.team.touchprotector.nolockapps.b;
import biz.obake.team.touchprotector.tpas.TPAccessibilityService;
import biz.obake.team.touchprotector.tpservice.TPService;
import java.util.ArrayList;
import java.util.List;

public class k extends RelativeLayout implements SharedPreferences.OnSharedPreferenceChangeListener, c.b {
    private static k l;

    /* renamed from: b  reason: collision with root package name */
    private Context f1637b;

    /* renamed from: c  reason: collision with root package name */
    private WindowManager f1638c;

    /* renamed from: d  reason: collision with root package name */
    private String f1639d = "no_preview";
    private List<l> e = new ArrayList();
    private g f;
    private e g;
    private j h;
    private n i;
    private boolean j = true;
    private m k = new m();

    private k(Context context) {
        super(context);
        this.f1637b = context;
        this.f1638c = (WindowManager) context.getSystemService("window");
        c();
    }

    public static void a() {
        k kVar = l;
        if (kVar != null) {
            kVar.q(true);
        }
    }

    private WindowManager.LayoutParams b() {
        int i2;
        int i3 = Build.VERSION.SDK_INT >= 26 ? 2038 : 2010;
        int i4 = (a.g("prevent_sleep") ? 128 : 0) | 256;
        if (this.j) {
            i4 |= 8;
        }
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(i3, i4, -3);
        int j2 = a.j("backlight");
        if (this.h.j()) {
            j2 = -1;
        }
        float f2 = 0.01f;
        if (j2 >= 0 && j2 < 30) {
            layoutParams.screenBrightness = 0.01f;
            layoutParams.flags |= 2;
            layoutParams.dimAmount = 1.0f - (((float) j2) / 30.0f);
        } else if (30 > j2 || j2 > 100) {
            layoutParams.screenBrightness = -1.0f;
        } else {
            float f3 = ((float) (j2 - 30)) / 70.0f;
            if (f3 >= 0.01f) {
                f2 = 1.0f < f3 ? 1.0f : f3;
            }
            layoutParams.screenBrightness = f2;
        }
        if (a.g("fix_orientation")) {
            int rotation = this.f1638c.getDefaultDisplay().getRotation();
            if (rotation != 1) {
                if (rotation == 2) {
                    i2 = 9;
                } else if (rotation != 3) {
                    layoutParams.screenOrientation = 1;
                } else {
                    i2 = 8;
                }
                layoutParams.screenOrientation = i2;
            } else {
                layoutParams.screenOrientation = 0;
            }
        }
        return layoutParams;
    }

    private void c() {
        a.a(this.f1637b).inflate(R.layout.protector_view, this);
        List<l> list = this.e;
        n nVar = new n(this);
        this.i = nVar;
        list.add(nVar);
        this.e.add(new i(this));
        List<l> list2 = this.e;
        g gVar = new g(this);
        this.f = gVar;
        list2.add(gVar);
        List<l> list3 = this.e;
        e eVar = new e(this);
        this.g = eVar;
        list3.add(eVar);
        List<l> list4 = this.e;
        j jVar = new j(this);
        this.h = jVar;
        list4.add(jVar);
        this.e.add(new c(this));
        this.e.add(new h(this));
        this.e.add(new b(this));
        this.e.add(new f());
    }

    private boolean d() {
        return "com.amazon.avod.thirdpartyclient".equals(c.c("TPAS.FrontApp"));
    }

    private boolean e() {
        Point point = new Point();
        this.f1638c.getDefaultDisplay().getRealSize(point);
        int width = getWidth();
        int height = getHeight();
        return (width == point.x && height == point.y) || (width == point.y && height == point.x);
    }

    private boolean f() {
        return "com.netflix.mediaclient".equals(c.c("TPAS.FrontApp"));
    }

    private void h() {
        String c2 = c.c("TPAS.FrontApp");
        biz.obake.team.touchprotector.nolockapps.a a2 = b.b().a(c2);
        if (a2 != null && a2.f1772a) {
            TPService.k("Unlock", "NoLockApps: " + c2);
        }
    }

    private void i() {
        if (this.j && !f() && !d() && !e()) {
            this.j = false;
            s();
        }
    }

    public static boolean j() {
        k kVar = l;
        return kVar != null && kVar.k();
    }

    private boolean k() {
        return this.g.n();
    }

    public static void l(Context context, String str) {
        if (l == null) {
            l = new k(new ContextThemeWrapper(context, R.style.AppTheme));
        }
        l.m(str);
    }

    private void m(String str) {
        if (getParent() == null) {
            if (str == null) {
                str = "no_preview";
            }
            this.f1639d = str;
            for (l d2 : this.e) {
                d2.d();
            }
            this.j = TPAccessibilityService.i();
            s();
            a.i().registerOnSharedPreferenceChangeListener(this);
            c.e(this);
        }
    }

    public static boolean n() {
        k kVar = l;
        return kVar != null && kVar.o();
    }

    private boolean o() {
        this.g.m();
        return this.h.m();
    }

    public static void p() {
        k kVar = l;
        if (kVar != null) {
            kVar.q(false);
        }
    }

    private void q(boolean z) {
        if (getParent() != null) {
            for (l a2 : this.e) {
                a2.a();
            }
            setSystemUiVisibility(0);
            if (z) {
                this.f1638c.removeViewImmediate(this);
            } else {
                this.f1638c.removeView(this);
            }
            a.i().unregisterOnSharedPreferenceChangeListener(this);
            c.h(this);
        }
    }

    private void r(WindowManager.LayoutParams layoutParams) {
        if (getParent() == null) {
            this.f1638c.addView(this, layoutParams);
        } else {
            this.f1638c.updateViewLayout(this, layoutParams);
        }
    }

    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
        if (biz.obake.team.touchprotector.d.e.b.c(keyEvent.getAction(), keyEvent.getKeyCode(), keyEvent.getRepeatCount(), keyEvent.isLongPress(), keyEvent.getEventTime())) {
            return true;
        }
        return super.dispatchKeyEvent(keyEvent);
    }

    public boolean g() {
        return !"no_preview".equals(this.f1639d);
    }

    public String getPreviewMode() {
        return this.f1639d;
    }

    /* access modifiers changed from: protected */
    public void onLayout(boolean z, int i2, int i3, int i4, int i5) {
        super.onLayout(z, i2, i3, i4, i5);
        i();
    }

    public void onRamPrefsChanged(String str) {
        if (((str.hashCode() == 175455640 && str.equals("TPAS.FrontApp")) ? (char) 0 : 65535) == 0) {
            i();
            h();
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:12:0x0027 A[ADDED_TO_REGION] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void onSharedPreferenceChanged(android.content.SharedPreferences r3, java.lang.String r4) {
        /*
            r2 = this;
            int r3 = r4.hashCode()
            r0 = 635648805(0x25e33b25, float:3.94183E-16)
            r1 = 1
            if (r3 == r0) goto L_0x001a
            r0 = 1346840719(0x5047288f, float:1.33652961E10)
            if (r3 == r0) goto L_0x0010
            goto L_0x0024
        L_0x0010:
            java.lang.String r3 = "backlight"
            boolean r3 = r4.equals(r3)
            if (r3 == 0) goto L_0x0024
            r3 = 1
            goto L_0x0025
        L_0x001a:
            java.lang.String r3 = "lock_frame_opacity"
            boolean r3 = r4.equals(r3)
            if (r3 == 0) goto L_0x0024
            r3 = 0
            goto L_0x0025
        L_0x0024:
            r3 = -1
        L_0x0025:
            if (r3 == 0) goto L_0x002a
            if (r3 == r1) goto L_0x002a
            goto L_0x002d
        L_0x002a:
            r2.s()
        L_0x002d:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: biz.obake.team.touchprotector.f.k.onSharedPreferenceChanged(android.content.SharedPreferences, java.lang.String):void");
    }

    /* access modifiers changed from: protected */
    public void onSizeChanged(int i2, int i3, int i4, int i5) {
        super.onSizeChanged(i2, i3, i4, i5);
        f.d().b();
        this.f.c();
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        for (l b2 : this.e) {
            if (b2.b(motionEvent)) {
                return true;
            }
        }
        return super.onTouchEvent(motionEvent);
    }

    public void onWindowFocusChanged(boolean z) {
        super.onWindowFocusChanged(z);
        c.g("NotificationDrawerCloser.Trigger", "pv");
    }

    /* access modifiers changed from: protected */
    public void onWindowVisibilityChanged(int i2) {
        if (i2 == 0 || i2 == 4) {
            m mVar = this.k;
            if (mVar != null) {
                mVar.m(this);
            }
        } else if (i2 == 8) {
            biz.obake.team.touchprotector.d.e.b.h();
            m mVar2 = this.k;
            if (mVar2 != null) {
                mVar2.o();
            }
        }
    }

    public void s() {
        for (l c2 : this.e) {
            c2.c();
        }
        r(b());
        this.i.h();
        String str = this.j ? "Half" : "Full";
        String c3 = c.c("TPAS.FrontApp");
        biz.obake.team.touchprotector.log.a b2 = biz.obake.team.touchprotector.log.a.b();
        b2.d("PV: " + str + ", " + c3);
    }
}
